package net.wanho.shop.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.wanho.shop.po.Category;
import net.wanho.shop.service.CategoryService;
import net.wanho.shop.vo.AjaxResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;


/**
 * <p>
 * 分类表 前端控制器
 * </p>
 *
 * @author 猛虎王
 * @since 2024-12-24
 */
@Api(tags = "07.分类表接口")
@RestController
@RequestMapping("/categories")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    @ApiOperation("分类表分页查询")
    @GetMapping
    public AjaxResult findPage(@ApiParam("页码") @RequestParam(required = false) Integer pageNum,
                               @ApiParam("页大小") @RequestParam(required = false) Integer pageSize,
                               @ApiParam("级别") @RequestParam(defaultValue = "3") Integer type) {
        Object obj = categoryService.find(pageNum, pageSize, type);
        return AjaxResult.success(obj);
    }


    @ApiOperation("根据主键查询分类表")
    @GetMapping("/{catId}")
    public AjaxResult findById(@PathVariable Serializable catId) {
        return AjaxResult.success(categoryService.findById(catId));
    }

    @ApiOperation("新增分类表")
    @PostMapping
    public AjaxResult add(@RequestBody Category category) {
        categoryService.add(category);
        return AjaxResult.success();
    }

    @ApiOperation("修改分类表")
    @PutMapping("/{catId}")
    public AjaxResult modify(@RequestBody Category category, @PathVariable Integer catId) {
        categoryService.modifyByCatId(category, catId);
        return AjaxResult.success();
    }

    @ApiOperation("根据主键删除分类表")
    @DeleteMapping("/{catId}")
    public AjaxResult removeById(@PathVariable Integer catId) {
        categoryService.removeById(catId);
        return AjaxResult.success();
    }

}
